//
//  AppDelegate.m
//  onebuycheckstand
//
//  Created by xu yao on 2021/3/1.
//

#import "AppDelegate.h"
#import <UPaySDK/UPaySDK.h>
//#import "UPaySDK.h"
#import "WXApi.h"
#import <AlipaySDK/AlipaySDK.h>
#import <GZRCpaySDK/GZRCpaySDK.h>
#import "ViewController.h"

#define APP_ID @"wx7e43babdf8adb602"
#define kWXAppSecret @"cafad1a09744da9b3f492a16626db7f2"
#define UNIVERSAL_LINK @"https://uppweb.gcongo.com.cn/"

@interface AppDelegate () <WXApiDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    if (@available(iOS 13.0, *)) {
        
    } else {
        // Fallback on earlier versions
        self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        [self.window makeKeyAndVisible];
        self.window.backgroundColor = [UIColor whiteColor];
        ViewController *view = [[ViewController alloc] init];
        UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:view];
        [self.window makeKeyAndVisible];
        self.window.rootViewController = nvc;
    }
    //注册SDK
    [UPaySDK registerActivePlatforms:@[@(USDKPlatformTypeWeChat),@(USDKPlatformTypeAliPay),@(USDKPlatformTypeGZRCloud)] onImport:^(USDKPlatformType platformType) {
        switch (platformType) {
            case USDKPlatformTypeWeChat:
                [USDKConnector connectWeChat:[WXApi class] delegate:self];
                break;
            case USDKPlatformTypeAliPay:
                [USDKConnector connectAliPay:[AlipaySDK class]];
                break;
            case USDKPlatformTypeGZRCloud:
                [USDKConnector connectGZRCpay:[GZRCpaySDK class]];
            default:
                break;
        }
    } onConfiguration:^(USDKPlatformType platformType, NSMutableDictionary *appInfo) {
        /** 配置第三方SDK账号 */
        switch (platformType) {
            case USDKPlatformTypeWeChat:
                [appInfo USDKSetupWeChatByAppId:APP_ID appSecret:kWXAppSecret universalLink:UNIVERSAL_LINK];
                break;
            case USDKPlatformTypeAliPay:
                [appInfo USDKSetupAliPayFromScheme:@"com.gogbuy.uppv2.biz.ios.app.demo"];
                break;
            case USDKPlatformTypeGZRCloud:
                [appInfo USDKSetupGZRCpayByAppNo:@"81788980201911059999999" andMerNo:@"SH3090000000009"];
                break;
            default:
                break;
        }
    }];
    
    return YES;
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    [UPaySDK handleOpenURL:url];
//    [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
//        NSString *message = resultDic[@"memo"];
//        NSString *resultStatus = resultDic[@"resultStatus"];
//        if ([resultStatus isEqualToString:@"9000"]) {
//            [[NSNotificationCenter defaultCenter] postNotificationName:@"TopupPaySuccess" object:nil userInfo:nil];
//            message = @"支付成功";
//        }
////        if (![message isEqualToString:@""]) {
//            UIAlertController *alertCtl = [UIAlertController alertControllerWithTitle:@"温馨提示" message:message preferredStyle:UIAlertControllerStyleAlert];
//            [alertCtl addAction: [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//
//            }]];
//            [self.window.rootViewController presentViewController:alertCtl animated:YES completion:nil];
//
////        }
//    }];
//    BOOL result =  NO;
//    if ([url.scheme hasPrefix:@"wx"] && [url.host isEqualToString:@"pay"]) {
//        [WXApi handleOpenURL:url delegate:self];
//    }
//    if ([url.host isEqualToString:@"safepay"]) {
//
//    }
    //黔农云支付结果回调，请以查询结果为准
    [[GZRCpaySDK defaultService] paymentResult:url callback:^(id  _Nonnull responseObject) {
        NSLog(@"黔农云支付结果：%@",responseObject);
    }];
    
    return YES;
}

- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
    // Called when a new scene session is being created.
    // Use this method to select a configuration to create the new scene with.
    return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
}


- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
    // Called when the user discards a scene session.
    // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
    // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}


@end
